package com.astoolkit.workflow.inputfilter
{
	import flash.utils.getQualifiedClassName;
	import com.astoolkit.workflow.inputfilter.api.ITaskInputFilter;
	import com.astoolkit.workflow.inputfilter.api.ITaskInputFilterFactory;

	public class DefaultTaskInputFilterFactory implements ITaskInputFilterFactory
	{
		private var _cache : Object;
		
		public function DefaultTaskInputFilterFactory()
		{
			_cache = {}; 
		}
		
		public function getInstanceForType( inObject : Object ) : ITaskInputFilter
		{
			var filter : ITaskInputFilter;
			var className : String = getQualifiedClassName( inObject );
			if( _cache.hasOwnProperty( className ) )
				filter = _cache[ className ] as ITaskInputFilter;
			else
			{
				if( inObject is String )
					filter = new StringInputFilter();
				else if( inObject is Function )
					filter = new FunctionReferenceTaskInputFilter();
				else if( inObject is ITaskInputFilter )
					filter = ITaskInputFilter( inObject );
				else if( inObject is Class )
					filter = ITaskInputFilter( new inObject() );
				else
					throw new Error( 
						"taskInputFilter must be either a String" +
						"representing a properties chain separated " +
						"by '.', a reference to a function" +
						"with signature function( inData : *, inTask : IWorkflowTask = null ) : Object, " +
						"an instance of ITaskInputFilter or a reference to" +
						"a class implementing ITaskInputFilter" );
				_cache[ className ] = filter;
			}
			
			return filter;
		}
	}
}